//
//  InManagementViewController.m
//  我的管理
//
//  Created by brother on 16/3/1.
//  Copyright © 2016年 brother. All rights reserved.
//

#import "InManagementViewController.h"
#import "MBProgressHUD.h"
#import "InManagerTableHeaderView.h"
#import "InLocalCacheViewController.h"


@interface InManagementViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UIImageView *userIconImageView;

@property (nonatomic, strong) IBOutlet InManagerTableHeaderView *heade;


//@property (nonatomic,assign)NSInteger index;
//懒加载数组
@property (nonatomic, strong)NSArray *dataArray;
//模型属性
@property (nonatomic, strong) UserModel *manageUserModel;
// 进度条的view
@property (nonatomic,weak) UIImageView *iconImageView;
//登录状态
@property(nonatomic,assign)BOOL isLogin;

//@property (nonatomic,weak) UIButton *nameBtn;

@end

static NSString *cellIdentifier = @"ManagermentCell";

@implementation InManagementViewController
//懒加载
- (NSArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = @[@"",@"订阅的频道",@"离线管理",@"我喜欢的",@"关于我们",@"意见反馈",@"退出"];
    }
    return _dataArray;
}

- (void)notificationCenter:(NSNotification *)sender {

    self.manageUserModel = sender.userInfo[@"usermadel"];
}

//  剪圆
- (void)clipImageCorrner {
   
    self.userIconImageView.layer.borderColor = [[UIColor lightGrayColor]CGColor];
    self.userIconImageView.layer.borderWidth = 5;
    self.userIconImageView.layer.cornerRadius = self.userIconImageView.frame.size.width*0.5;
    self.userIconImageView.clipsToBounds = YES;
    
    self.userIconImageView.layer.cornerRadius = self.userIconImageView.frame.size.height*0.5;
    self.userIconImageView.clipsToBounds = YES;
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    NSString *user_id = [userDefault valueForKey:@"u_id"];
    if (user_id != nil) {
        [self.myTableView reloadData];
    }
    
    [self denglu];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //    剪圆
    [self clipImageCorrner];
//    tableView 的 背景
    UIImageView *backImageView=[[UIImageView alloc]initWithFrame:self.view.bounds];
    [backImageView setImage:[UIImage imageNamed:@"1212.png"]];
    self.myTableView.backgroundView=backImageView;

    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    //通过Nib生成cell，然后注册 Nib的view需要继承 UITableViewCell
    [self.myTableView registerNib:[UINib nibWithNibName:@"InManagermentTableViewCell" bundle:nil] forCellReuseIdentifier:cellIdentifier
     ];
    
  //  NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    
   // [self denglu];
    // Do any additional setup after loading the view from its nib.
}

- (void)denglu {
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    NSString *user_id = [userDefault valueForKey:@"u_id"];
    if (user_id == nil) {
        if (![AppDelegate shareAppdelegate].userModel) {
            NSString *imgPath = [[NSUserDefaults standardUserDefaults]objectForKey:@"icon"];
            
            NSLog(@"  imgPath   %@",imgPath);
            
            NSDictionary *dict = [[NSUserDefaults standardUserDefaults]objectForKey:@"userDict"];
            if (imgPath) {
                //             注册界面保存的
                NSLog(@"管理界面接受的dict%@",dict);
                [self.userIconImageView sd_setImageWithURL:dict[@"icon"]];
//                self.userIconImageView.image = [UIImage imageWithContentsOfFile:dict[@"icon"]];
                self.userIconImageView.image = [UIImage imageWithContentsOfFile:imgPath];
                //            [self.usernameBtn setTitle:dict[@"name"] forState:UIControlStateNormal];
                self.nameLabel.text = dict[@"name"];
               // self.isLogin = YES;
                
            }else {
                NSString *imageViewUrl = [AppDelegate shareAppdelegate].userModel.icon;
                
                
                [self.userIconImageView sd_setImageWithURL:[NSURL URLWithString:imageViewUrl]];
                self.nameLabel.text = [AppDelegate shareAppdelegate].userModel.name;
//                self.isLogin = YES;
            }
        }
        else {
            NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
            NSString *name = [defaults objectForKey:@"name"];//根据键值取出name
            NSLog(@"我的管理主页 %@",name);
            //        登录调用的
            UserModel *user = [AppDelegate shareAppdelegate].userModel;
            [self.userIconImageView sd_setImageWithURL:[NSURL URLWithString:user.icon]];
            self.nameLabel.text = user.name;
           
            NSLog(@"我的管理主页%@",user.name);
            NSLog(@"我的管理主页%@",user.icon);
          //  self.isLogin = YES;
            [self.myTableView reloadData];
        }
        [self.myTableView reloadData];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 100;
    }else {
        return 50;
    }
}
#pragma mark - 分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

#pragma mark - 几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

#pragma mark - 显示的东西
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    NSString *user_id = [userDefault valueForKey:@"u_id"];
    
    InManagermentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    cell.backgroundColor=[UIColor clearColor];
    
    if (indexPath.row == 0) {
        cell.imageView.image = [UIImage imageNamed:@"xiugai.png"];
        if (user_id == nil) {
            cell.textLabel.text = @"请点击登陆";
        }else{
            cell.textLabel.text = @"修改密码";
        }
    }
    if (indexPath.row == 1) {
        cell.imageView.image = [UIImage imageNamed:@"dingyue.png"];
        cell.textLabel.text = self.dataArray[indexPath.row];
    }
    if (indexPath.row == 2) {
        cell.imageView.image = [UIImage imageNamed:@"lixianwenjian.png"];
        cell.textLabel.text = self.dataArray[indexPath.row];
    }
    if (indexPath.row == 3) {
        cell.imageView.image = [UIImage imageNamed:@"woxihuande.png"];
        cell.textLabel.text = self.dataArray[indexPath.row];
    }
    if (indexPath.row == 4) {
        cell.imageView.image = [UIImage imageNamed:@"guanyuwomen.png"];
        cell.textLabel.text = self.dataArray[indexPath.row];
    }
    if (indexPath.row == 5) {
        cell.imageView.image = [UIImage imageNamed:@"yijianfankui.png"];
        cell.textLabel.text = self.dataArray[indexPath.row];
    }
    if (indexPath.row == 6) {
        cell.imageView.image = [UIImage imageNamed:@"tuichu.png"];
        cell.textLabel.text = self.dataArray[indexPath.row];
    }
    return cell;
}

#pragma mark - 判断 isLogin   和点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    NSString *user_id = [userDefault valueForKey:@"u_id"];
    
    if (indexPath.row == 0) {
//           if(![AppDelegate shareAppdelegate].userModel){
        if (user_id == nil) {

               InLoginViewController *contro = [[InLoginViewController alloc]initWithNibName:@"InLoginViewController" bundle:nil];
               [self presentViewController:contro animated:YES completion:nil];
               NSLog(@"登陆/注册");
           }else {
               InXiugaiViewController *conro = [[InXiugaiViewController alloc]initWithNibName:@"InXiugaiViewController" bundle:nil];
               [self presentViewController:conro animated:YES completion:nil];
               NSLog(@"修改信息");
           }
    }else {
    if (user_id == nil) {
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请登录后进入" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alter show];
    }else{
        
        if (indexPath.row == 1) {
            
            InToChancelViewController *contro = [[InToChancelViewController alloc]initWithNibName:@"InToChancelViewController" bundle:nil];
            
            [self presentViewController:contro animated:YES completion:nil];
        }
        if (indexPath.row == 2) {
            InLocalCacheViewController *control = [[InLocalCacheViewController alloc]initWithNibName:@"InLocalCacheViewController" bundle:nil];
            
            [self presentViewController:control animated:YES completion:nil];
        }
        if (indexPath.row == 3) {
            InFavouriteViewController *control = [[InFavouriteViewController alloc]initWithNibName:@"InFavouriteViewController" bundle:nil];
            [self presentViewController:control animated:YES completion:nil];
        }
        if (indexPath.row == 4) {
            InAboutUsViewController *control = [[InAboutUsViewController alloc]initWithNibName:@"InAboutUsViewController" bundle:nil];
            
            [self presentViewController:control animated:YES completion:nil];
        }
        if (indexPath.row == 5) {
            InFeedBackViewController *control = [[InFeedBackViewController alloc]initWithNibName:@"InFeedBackViewController" bundle:nil];
            
            [self presentViewController:control animated:YES completion:nil];
        }
        if (indexPath.row == 6) {
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确定注销?" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
            [alter show];
        }
    }
    }
}

#warning 注销的点击事件
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault removeObjectForKey:@"u_id"];
    
    NSLog(@"   userdefault    %@",[userDefault valueForKey:@"u_id"]);
    if ([userDefault valueForKey:@"u_id"] == nil) {
        self.userIconImageView.image = nil;
        
        self.nameLabel.text = nil;
        
        [self.myTableView reloadData];
    }
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
